package com.prp.gwt.client.widgets.admin;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.vo.UserList;

public class CommunityMember extends VerticalPanel {

	private static CommunityMember instance;
	private HTML id = new HTML("");
	private HTML size = new HTML("");
	private FlexTable table = new FlexTable();
	HorizontalPanel topPanel = new HorizontalPanel();

	public static CommunityMember getInstance() {
		if (instance == null)
			instance = new CommunityMember();
		return instance;
	}

	private CommunityMember() {
		HTML returnButton = new HTML("<a><<返回</a>");
		returnButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				AdminView.getInstance().showWidget(AdminView.COMMUNITYLIST);
			}
		});
		add(returnButton);

		topPanel.add(new HTML("社区ID："));
		topPanel.add(id);
		topPanel.add(new HTML("社区人数："));
		topPanel.add(size);
		topPanel.setSpacing(10);
		table.setCellPadding(20);
		add(topPanel);
		add(table);
	}

	public void update(UserList list, int community_id) {
		int n = table.getRowCount();
		for (int i = n - 1; i >= 0; i--)
			table.removeRow(i);
		if (community_id >= 0) {
			size.setText(String.valueOf(list.size()));
			id.setText(String.valueOf(community_id));
			topPanel.setVisible(true);
		}
		if (community_id < 0 && list.size() == 0) {
			table.setHTML(0, 0, "所有用户均已划分入社区");
			return;
		}
		int row = 0;
		if (community_id >= 0)
			table.setHTML(row++, 0, "核心成员:");
		table.setHTML(row, 0, "邮箱");
		table.setHTML(row++, 1, "用户名");
		for (int i = 0; i < list.size(); i++) {
			if (list.get(i).isCore()) {
				table.setHTML(row, 0, list.get(i).getEmail());
				table.setHTML(row, 1, list.get(i).getUsername());
				row++;
			}
		}
		if (community_id >= 0) {
			table.setHTML(row++, 0, "非核心成员:");
			table.setHTML(row, 0, "邮箱");
			table.setHTML(row++, 1, "用户名");
		}
		for (int i = 0; i < list.size(); i++) {
			if (!list.get(i).isCore()) {
				table.setHTML(row, 0, list.get(i).getEmail());
				table.setHTML(row, 1, list.get(i).getUsername());
				row++;
			}
		}
	}

	public void reset() {
		topPanel.setVisible(false);
		int n = table.getRowCount();
		for (int i = n - 1; i >= 0; i--)
			table.removeRow(i);
	}

}
